package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/11/2 11:51
 * @question:
 */

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页信息条件查询
     * @param page
     * @param pageSize
     */
    @Override
    public Page<Category> selectPage(int page,int pageSize) {

        Page<Category> pageInfo = new Page<>(page,pageSize);
        //使用条件查询 将分类信息进行排序
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //调用mapper查询
        categoryMapper.selectPage(pageInfo,queryWrapper);

        return pageInfo;
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void removeId(Long id) {

        //1.查看菜品和套餐类的关联id有没有值
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        //2.调用count方法
        int count1 = dishMapper.selectCount(dishLambdaQueryWrapper);
        int count2 = setmealMapper.selectCount(setmealLambdaQueryWrapper);

        //3.如果count>0,则抛出一个自定义业务异常
        if (count1 > 0) {
            //不能删除
            throw new RuntimeException("此类下关联了菜品,不能直接进行删除!");
        }

        if (count2 > 0) {
            //不能删除
            throw new RuntimeException("此类下关联了套餐,不能直接进行删除!");
        }

        //4.到此处 则该id下的分类既没有菜品也没有套餐 可以删除
        this.removeById(id);
    }

    /**
     * 下拉列表的展示
     * @param category
     * @return
     */
    @Override
    public List<Category> listR(Category category) {

        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //对类型进行等值比较
        wrapper.eq(category.getType() != null,Category::getType,category.getType());
        //排序
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //调用service查询
        List<Category> list = categoryMapper.selectList(wrapper);

        return list;
    }


}
